package com.bosssoft.ct.springcloud.controller;

import com.bosssoft.ct.springcloud.utils.http.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 获取该服务信息-逻辑控制器
 * @author ChenTong
 * @version 1.0
 * @date 2020/6/22 19:53
 * @copyright
 * @modified
 * @see
 * @since
 **/
@Slf4j
@RestController
public class DiscoveryController {
    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private int serverPort;

    @GetMapping(value = "/discovery")
    public HttpResult discovery(){
        List<String> services =discoveryClient.getServices();
        for (String element: services
             ) {
            log.info("element: {}",element);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-ADMIN-SERVICE");
        for (ServiceInstance instance: instances
        ) {
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }

        return HttpResult.ok(serverPort);
    }
}
